package com.jinyu.jyly.ui.mine.address;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.jinyu.jyly.R;
import com.jinyu.jyly.adapter.ShipAddressListAdapter;
import com.jinyu.jyly.base.BaseActivity;
import com.jinyu.jyly.bean.AddressList;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.utils.AES;
import com.jinyu.jyly.utils.DisplayCutoutUtil;
import com.jinyu.jyly.view.GridSpacingItemDecoration;


import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 收货信息
 */
public class ShipAddressListActivity extends BaseActivity<ShipAddressListPresenter> implements ShipAddressListContract.View, View.OnClickListener {

    @BindView(R.id.fake_status_bar)
    View fake_status_bar;
    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.tv_add)
    TextView tv_add;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.ll_empty)
    LinearLayout ll_empty;
    @BindView(R.id.tv_add_address)
    TextView tv_add_address;

    private ShipAddressListPresenter mPresenter;


    @Override
    public int getLayout() {
        return R.layout.activity_ship_address_list;
    }

    @Override
    protected ShipAddressListPresenter getPresenter() {
        if (mPresenter == null) {
            mPresenter = new ShipAddressListPresenter(this, this);
        }
        return mPresenter;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getAddress();
    }

    @Override
    protected void initEventAndData() {
        QMUIStatusBarHelper.translucent(this);
        QMUIStatusBarHelper.setStatusBarDarkMode(this);
        fitterScreen();
        initRecycleView();

    }



    private void initRecycleView() {
        tv_add_address.setOnClickListener(this);
        tv_add.setOnClickListener(this);
        iv_back.setOnClickListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(3, 30, false));
        mRecyclerView.setItemViewCacheSize(10);
    }

    /**
     * 适配刘海
     */
    private void fitterScreen() {
        if (mIsNeed) {
            int pH = DisplayCutoutUtil.getStatusBarHeight(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.height = pH;
            fake_status_bar.setLayoutParams(params);
        }
    }


    /**
     * 获取收获地址列表
     *
     * @param info
     */
    @Override
    public void onAddressList(AllInfoBean info) {
        switch (info.getStatus()) {
            case 200:
                String infoData = info.getData();
                try {
                    String result = AES.decrypt(infoData);
                    Gson gson = new Gson();
                    Type listType = new TypeToken<ArrayList<AddressList>>() {}.getType();
                    List<AddressList> mList = gson.fromJson(result, listType);
                    if(mList != null && mList.size() != 0){
                        ll_empty.setVisibility(View.GONE);
                        mRecyclerView.setVisibility(View.VISIBLE);
                        ShipAddressListAdapter shipAddressListAdapter = new ShipAddressListAdapter(R.layout.item_ship_address, mList);
                        shipAddressListAdapter.setHasStableIds(true);
                        shipAddressListAdapter.openLoadAnimation();
                        mRecyclerView.setAdapter(shipAddressListAdapter);

                        shipAddressListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                            @Override
                            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                                switch (view.getId()){
                                    case R.id.tv_edit: //编辑
                                        AddressList addressList = mList.get(position);
                                        Intent mIntent = new Intent(ShipAddressListActivity.this,ShipUpdateAddressActivity.class);
                                        mIntent.putExtra("infoBean",addressList);
                                        startActivity(mIntent);

                                        break;
                                    default:break;
                                }
                            }
                        });

                    }else{
                        ll_empty.setVisibility(View.VISIBLE);
                        mRecyclerView.setVisibility(View.GONE);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                showToast(info.getMsg());
                break;
            default:
                break;
        }
    }


    @Override
    public void onFailer(Throwable throwable) {
        mTipDialog.dismiss();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_add_address:
            case R.id.tv_add:
                Intent mIntent = new Intent(ShipAddressListActivity.this,ShipUpdateAddressActivity.class);
                startActivity(mIntent);
                break;
            case R.id.iv_back:
                finish();
                break;
            default:
                break;
        }
    }
}
